<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT">










<meta name="description" content="第2章 Spring Boot 基础配置 本学习笔记来自《Spring Boot+Vue全栈开发实战》 王松著  @Spring BootApplication注解 @Spring BootApplication实际上是一个组合注解，由三个注解组成。   @Spring BootConfiguration，原来就是一个@Configuration，所以Spring BootConfiguratio">
<meta property="og:type" content="article">
<meta property="og:title" content="Spring Boot - 第二章 - Web 容器配置">
<meta property="og:url" content="https://mingmingv.gitee.io/2019/04/14/Spring-Boot-第二章-Web 容器配置/index.html">
<meta property="og:site_name" content="四 季">
<meta property="og:description" content="第2章 Spring Boot 基础配置 本学习笔记来自《Spring Boot+Vue全栈开发实战》 王松著  @Spring BootApplication注解 @Spring BootApplication实际上是一个组合注解，由三个注解组成。   @Spring BootConfiguration，原来就是一个@Configuration，所以Spring BootConfiguratio">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2019-04-14T09:47:43.306Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Spring Boot - 第二章 - Web 容器配置">
<meta name="twitter:description" content="第2章 Spring Boot 基础配置 本学习笔记来自《Spring Boot+Vue全栈开发实战》 王松著  @Spring BootApplication注解 @Spring BootApplication实际上是一个组合注解，由三个注解组成。   @Spring BootConfiguration，原来就是一个@Configuration，所以Spring BootConfiguratio">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://mingmingv.gitee.io/2019/04/14/Spring-Boot-第二章-Web 容器配置/">





  <title>Spring Boot - 第二章 - Web 容器配置 | 四 季</title>
  








</head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">四 季</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://mingmingv.gitee.io/2019/04/14/Spring-Boot-第二章-Web 容器配置/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="四 季">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="四 季">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Spring Boot - 第二章 - Web 容器配置</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-04-14T15:34:26+08:00">
                2019-04-14
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="第2章-Spring-Boot-基础配置"><a href="#第2章-Spring-Boot-基础配置" class="headerlink" title="第2章 Spring Boot 基础配置"></a>第2章 Spring Boot 基础配置</h1><blockquote>
<p>本学习笔记来自《Spring Boot+Vue全栈开发实战》 王松著</p>
</blockquote>
<h2 id="Spring-BootApplication注解"><a href="#Spring-BootApplication注解" class="headerlink" title="@Spring BootApplication注解"></a>@Spring BootApplication注解</h2><blockquote>
<p>@Spring BootApplication实际上是一个组合注解，由<strong>三个</strong>注解组成。</p>
</blockquote>
<ol>
<li>@Spring BootConfiguration，原来就是一个@Configuration，所以Spring BootConfiguration的功能就是表明这是一个配置类，开发者可以在这个类中配置Bean。</li>
<li>@EnableAutoConfiguration，表示开启自动化配置。Spring Boot中的自动化配置是非侵入式的，在任何时刻，开发者都可以使用自定义配置代替自动化配置中的某一个配置。</li>
<li>@ComponentScan完成包扫描，也是Spring中的功能。除了扫描@Service、@Repository、@Component、@Controller和RestController等之外，也会扫描@Configuration注解的类。</li>
</ol>
<h2 id="Web-容器配置"><a href="#Web-容器配置" class="headerlink" title="Web 容器配置"></a>Web 容器配置</h2><h3 id="一、Tomcat配置"><a href="#一、Tomcat配置" class="headerlink" title="一、Tomcat配置"></a>一、Tomcat配置</h3><ol>
<li>常规配置：在Spring Boot项目中，可以内置Tomcat、Jetty、Undertow、Netty等容器。当开发者添加了spring-boot-stater-web依赖后，默认会使用Tomcat作为Web。如需要对Tomcat做出进一步配置，可以在application.properties中进行配置，代码如下：</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">server.port=8080</span><br><span class="line">server.error.path=/error</span><br><span class="line">server.servlet.session.timeout=30m</span><br><span class="line">server.servlet.context-path=/chapter02</span><br><span class="line">server.tomcat.uri-encoding=utf-8</span><br><span class="line">server.tomcat.max-threads=500</span><br><span class="line">server.tomcat.basedir=/home/sang/tep</span><br></pre></td></tr></table></figure>
<h4 id="代码解释："><a href="#代码解释：" class="headerlink" title="代码解释："></a>代码解释：</h4><ul>
<li><strong>server.port</strong>：配置了Web容器的端口号。</li>
<li><strong>server.error.path</strong>：配置当项目出错时挑战去的页面。</li>
<li><strong>server.servlet.session.timeout</strong>：配置session失效时间，3om表示30分钟，若不写默认为<code>秒</code>。</li>
<li><strong>server.servlet.context-path：</strong>：表示项目名称，不配置是默认为<code>/</code>。</li>
<li><strong>server.tomcat.uri-encoding</strong>：表示配置Tomcat请求编码。</li>
<li><strong>server.tomcat.max-threads</strong>：表示配置Tomcat最大线程数。</li>
<li><strong>server.tomcat.basedir</strong>：是存放Tomcat运行日志和临时文件的目录，若不配置，则默认使用系统的临时目录。</li>
</ul>
<h3 id="2-HTTPS配置"><a href="#2-HTTPS配置" class="headerlink" title="2.HTTPS配置"></a>2.HTTPS配置</h3><ol>
<li>由于HTTPS具有良好的安全性，在开发中得到越来越广泛的应用，像微信公众号、小程序等开放都要使用HTTPS来完成。对于个人开发者而言，一个HTTPS证书的价格还是有点贵，国内有一些云服务器厂商提供免费的HTTPS证书。不过JDK中也提供了一个Java数字证书劵工具keytool，在\jdk\bin目录下，通过这个工具可以自己生成一个数字证书。</li>
<li>命令如下：</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">keytool -genkey -<span class="built_in">alias</span> tomcathttps -keyalg RSA -keysize 2048 -keystore sang.p12 -validity 365</span><br></pre></td></tr></table></figure>
<h4 id="命令解释："><a href="#命令解释：" class="headerlink" title="命令解释："></a>命令解释：</h4><ul>
<li><strong>-genkey</strong>： 表示要创建一个新的密钥</li>
<li><strong>-alias</strong>：表示keystore的别名</li>
<li><strong>-keyalg</strong>：表示使用的加密算法是RSA，一种非对称加密算法。</li>
<li><strong>-keysize</strong>：表示密钥的长度。</li>
<li><strong>-keystore</strong>：表示生成的密钥存放位置。</li>
<li><strong>-validity</strong>表示密钥的有效时间，单位为天。</li>
</ul>
<p><code>在cmd窗口中直接执行如上命令，在执行的过程中需要输入密钥口令等信息，根据提供输入即可，命令执行完成后，会在当前用户目录下生成一个名为sang.p12的文件，将这个文件复制到项目的根目录下，然后在application.properties中做如下配置：</code></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">server.ssl.key-store=sang.p12</span><br><span class="line">server.ssl.key-alias=tomcathttps</span><br><span class="line">server.ssl.key-store-password=123456</span><br></pre></td></tr></table></figure>
<h4 id="代码解释"><a href="#代码解释" class="headerlink" title="代码解释"></a>代码解释</h4><ul>
<li><strong>key-store</strong>：表示密钥文件名。</li>
<li><strong>key-alias</strong>：表示密钥别名。</li>
<li><strong>key-store-password</strong>：就是在cmd命令执行过程中输入的密码。</li>
</ul>
<p><code>配置后启动项目，因为证书是自己生成的，不被浏览器任何，此时添加信任或继续前进即可</code></p>
<h3 id="二、Jetty配置"><a href="#二、Jetty配置" class="headerlink" title="二、Jetty配置"></a>二、Jetty配置</h3><p><code>除了Tomcat外，也可以在Spring Boot中嵌入Jetty，配置方式如下：</code></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">&lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;</span><br><span class="line">    &lt;exclusions&gt;</span><br><span class="line">        &lt;exclusion&gt;</span><br><span class="line">            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;</span><br><span class="line">            &lt;artifactId&gt;spring-boot-starter-tomcat&lt;/artifactId&gt;</span><br><span class="line">        &lt;/exclusion&gt;</span><br><span class="line">    &lt;/exclusions&gt;</span><br><span class="line">&lt;/dependency&gt;</span><br><span class="line">&lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;spring-boot-starter-jetty&lt;/artifactId&gt;</span><br><span class="line">&lt;/dependency&gt;</span><br></pre></td></tr></table></figure>
<p><code>主要是从spring-boot-starter-web中除去默认的Tomcat，然后加入Jetty的依赖即可。</code></p>
<h3 id="三、Undertow-配置"><a href="#三、Undertow-配置" class="headerlink" title="三、Undertow 配置"></a>三、Undertow 配置</h3><p><code>Undertow是一个红帽公司开源的Java服务器，具有非常好的性能，在Spring Boot中也得到了很好的支持，配置方式与Jetty类似，则代码如下：</code></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">&lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;</span><br><span class="line">    &lt;exclusions&gt;</span><br><span class="line">        &lt;exclusion&gt;</span><br><span class="line">            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;</span><br><span class="line">            &lt;artifactId&gt;spring-boot-starter-tomcat&lt;/artifactId&gt;</span><br><span class="line">        &lt;/exclusion&gt;</span><br><span class="line">    &lt;/exclusions&gt;</span><br><span class="line">&lt;/dependency&gt;</span><br><span class="line">&lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;spring-boot-starter-undertow&lt;/artifactId&gt;</span><br><span class="line">&lt;/dependency&gt;</span><br></pre></td></tr></table></figure>
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/04/14/Spring-Boot-第一章/" rel="next" title="Spring Boot - 第一章">
                <i class="fa fa-chevron-left"></i> Spring Boot - 第一章
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/04/14/Spring-Boot-第二章-Properties等其他配置文件/" rel="prev" title="Spring Boot - 第二章 - Properties等其他配置文件">
                Spring Boot - 第二章 - Properties等其他配置文件 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/avatar.gif" alt="四 季">
            
              <p class="site-author-name" itemprop="name">四 季</p>
              <p class="site-description motion-element" itemprop="description">我的学习笔记站</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">15</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#第2章-Spring-Boot-基础配置"><span class="nav-number">1.</span> <span class="nav-text">第2章 Spring Boot 基础配置</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#Spring-BootApplication注解"><span class="nav-number">1.1.</span> <span class="nav-text">@Spring BootApplication注解</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Web-容器配置"><span class="nav-number">1.2.</span> <span class="nav-text">Web 容器配置</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#一、Tomcat配置"><span class="nav-number">1.2.1.</span> <span class="nav-text">一、Tomcat配置</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#代码解释："><span class="nav-number">1.2.1.1.</span> <span class="nav-text">代码解释：</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-HTTPS配置"><span class="nav-number">1.2.2.</span> <span class="nav-text">2.HTTPS配置</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#命令解释："><span class="nav-number">1.2.2.1.</span> <span class="nav-text">命令解释：</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#代码解释"><span class="nav-number">1.2.2.2.</span> <span class="nav-text">代码解释</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#二、Jetty配置"><span class="nav-number">1.2.3.</span> <span class="nav-text">二、Jetty配置</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#三、Undertow-配置"><span class="nav-number">1.2.4.</span> <span class="nav-text">三、Undertow 配置</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">四 季</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
